import router from './router'
import store from './store'
router.beforeEach(async(to, from, next) => {
  const token = store.getters.token
  const whiteList = ['/login', '/404']
  if (token) {
    if (to.path === '/login') {
      next('/')
    } else {
      if (!store.getters.name) {
        await store.dispatch('user/getUserInfo')
        const menus = store.state.user.userInfo.roles.menus
        const routes = await store.dispatch('permission/filterRoutes', menus)
        // console.log('141414', routes)
        router.addRoutes([
          ...routes,
          { path: '*', redirect: '/404', hidden: true }
        ])
        next(to.path)
        return
      }
      next()
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next('/login')
    }
  }
})
